class ImportCategoriesDrugs < ActiveRecord::Migration
  def self.up
    sql = <<-SQL
      select id, category_name from askapatient.categories
    SQL
    categories = select_all(sql)
    categories.each do |category|
      Category.create(:id => category["id"], :name => category["category_name"])
    end
    sql = <<-SQL
      select id, drug_name from askapatient.drugs
    SQL
    drugs = select_all(sql)
    drugs.each do |drug|
      Drug.create(:id => drug["id"], :name => drug["drug_name"])
    end
    sql = <<-SQL
      select category_id, drug_id from askapatient.category_drugs
    SQL
    categories_drugs = select_all(sql)
    categories_drugs.each do |category_drug|
      sql = <<-SQL
        insert into categories_drugs (category_id, drug_id) values (#{category_drug["category_id"]}, #{category_drug["drug_id"]})
      SQL
      execute(sql)
    end
  end

  def self.down
    sql = <<-SQL
      delete from categories_drugs
    SQL
    execute(sql)
    Category.delete_all
    Drug.delete_all
  end
end
